﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace schedule
{
    public partial class EventList : Form
    {
        // 달력에서 선택 한 날짜.
        private DateTime selectTime;

        // 해당 날짜가 가지고 있는 할일들.
        private List<WorkInfo> workinfoList = new List<WorkInfo>();

        public enum ReturnType
        {
            ReturnType_Make,
            ReturnType_Modify,
            ReturnType_Cancel,
        }
        public ReturnType returnType;

        public EventList()
        {
            InitializeComponent();
            returnType = ReturnType.ReturnType_Cancel;
        }

        public void setDate(DateTime dateTime)
        {
            selectTime = dateTime;
        }

        private void EventList_Load(object sender, EventArgs e)
        {
            // 리스트 박스 채우기, 해당 날짜(selectTime)에 할 일이 있으면 리스트 박스에 추가 한다.
            bool existWorkInfo = false;
            WorkManager wm = WorkManager.Instance();
            foreach (WorkInfo wi in wm.workinfoList)
            {
                if (wm.isMyGroup(wi) == false)
                {
                    continue;
                }

                long selectTick = selectTime.Ticks;
                long startTick = wi.startTime.Ticks;
                long endTick = wi.endTime.Ticks;
                if (startTick <= selectTick && selectTick <= endTick)
                {
                    string text = "[" + wi.owner + "] ";
                    text += wi.title;
                    text += string.Format(" ({0}/{1}/{2} - {3}/{4}/{5})",
                        wi.startTime.Year, wi.startTime.Month, wi.startTime.Day,
                        wi.endTime.Year, wi.endTime.Month, wi.endTime.Day );
                    lbWorkList.Items.Add(text);
                    workinfoList.Add(wi);
                    existWorkInfo = true;
                }
            }

            btn_EListModify.Enabled = false;
            btnDelete.Enabled = false;
            if (existWorkInfo)
            {
            }
            else
            {
                lbWorkList.Enabled = false;
            }

        }

        private void lbWorkList_MouseDown(object sender, MouseEventArgs e)
        {
            int index = lbWorkList.SelectedIndex;
            if (index == -1)
            {
                btn_EListModify.Enabled = false;
                btnDelete.Enabled = false;
            }
            else
            {
                btn_EListModify.Enabled = true;
                btnDelete.Enabled = true;
            }
        }

        private void modifyData()
        {
            int index = lbWorkList.SelectedIndex;
            if (index != -1)
            {
                MakeForm makeForm = new MakeForm(false);

                WorkInfo wi = workinfoList[index];
                makeForm.editContent(wi);
                if (makeForm.ShowDialog() == DialogResult.OK)
                {
                    // 수정!
                    WorkManager wm = WorkManager.Instance();
                    wm.changeWorkInfo(wi, makeForm);

                    returnType = ReturnType.ReturnType_Modify;
                    Close();
                }
            }
        }

        private void btn_EListModify_Click(object sender, EventArgs e)
        {
            modifyData();
        }

        private void btn_EListMake_Click(object sender, EventArgs e)
        {
            WorkManager wm = WorkManager.Instance();
            MakeForm makeForm = new MakeForm(false);
            DateTime clickTime = new DateTime(selectTime.Year, selectTime.Month, selectTime.Day);
            makeForm.setTime(clickTime);
            if (makeForm.ShowDialog() == DialogResult.OK)
            {
                // 수정!
                WorkInfo wi = new WorkInfo();
                wm.changeWorkInfo(wi, makeForm);
                wm.add(wi);

                returnType = ReturnType.ReturnType_Make;
                Close();
            }
        }

        private void bnt_EListCancel_Click(object sender, EventArgs e)
        {
            //MainForm.calendar.refreshScreen();
        }

        private void lbWorkList_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            modifyData();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            int index = lbWorkList.SelectedIndex;
            if (index != -1)
            {
                WorkManager wm = WorkManager.Instance();
                WorkInfo wi = workinfoList[index];

                if (wm.isMadeByMe(wi))
                {
                    wm.workinfoList.Remove(wi);
                }
                else
                {
                    wi.isJoin = false;
                }

                workinfoList.Remove(wi);
                lbWorkList.Items.RemoveAt(index);

                index = -1;
                btn_EListModify.Enabled = false;
                btnDelete.Enabled = false;
            }
        }

        private void EventList_FormClosing(object sender, FormClosingEventArgs e)
        {
            MainForm.calendar.refreshScreen();
        }

    }
}
